<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>数据模版定义规范 - Function</title>
</head>

<body>
    <script src="https://cdn.bootcdn.net/ajax/libs/Mock.js/1.0.1-beta3/mock-min.js"></script>
    <h2>F12打开控制台查看数据，效果更佳</h2>
    <div class="box">
        展示内容:<div id="content"></div>
        <div id="list_array"></div>
    </div>
    <script>
        let Random = Mock.Random;
        //生成数据
        list = Mock.mock({
            'foo': 'Syntax Demo',
            'name': function () {
                return this.foo
            },
        }),

            list_array = Mock.mock({
                'list|2-10': [{
                    'id': () => Random.integer(20, 100),
                    'name': () => Random.ctitle(),
                    'date': () => Random.date("yyyy-MM-dd")
                }]

                //这样写会产生重复的值
                //'list|2-10': [{
                //    'id': Random.integer(20, 100),
                //    'name': Random.ctitle(),
                //    'date': Random.date("yyyy-MM-dd")
                //}]

            })

        //展示数据
        console.table(list)
        console.log(list_array)
        x = document.getElementById("content");
        //将对象转为JSON
        x.innerHTML = JSON.stringify(list, null, '<hr />');

        document.getElementById("list_array").innerHTML = JSON.stringify(list_array, null, '<br />');
    </script>

</body>

</html>